//
//  CMDBMovieDetatilViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/8.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let kTitleViewH : CGFloat = 44

class CMDBMovieDetatilViewController: CMDBFullContentViewController {
    
    var topView : UIView!
    
    var movieCoverTopView : UIImageView!
    
    var detailContentView : UIView!
    
    lazy var pageTitleView : CMDBPageTitleView = {
    
       let titleFrame = CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: 44)
        
        let titltes   = ["职员","演员"]
        
        let titleView = CMDBPageTitleView(frame: titleFrame, titles: titltes)
        
        titleView.titleFont = CMDB_16_FONT
        
        titleView.backgroundColor = pageTitBackColor
        
        titleView.delegate = self
        
        return titleView
        
    }()
    
    lazy var pageContentView : CMDBPageContentView = {
        
        let contentH = CMDB_Screen_Height - kTitleViewH - 0.5 * CMDB_Screen_Width - 64
        let contentFrame = CGRect(x: 0, y: kTitleViewH, width: CMDB_Screen_Width, height: contentH)
        
        var childVcs = [UIViewController]()
        
        childVcs.append(CMDBMovieDetailStaffViewController())
        childVcs.append(CMDBTestViewController())
        
        let contentView = CMDBPageContentView(frame: contentFrame, childVcs: childVcs, parentController: self)
        contentView.delegate = self
        return contentView
        
    }()

    override func buildContentView() {
        super.buildContentView()
        
        buildTopView()
        
        buildDeatilContentView()
        
    }
}

//MARK: - 设置UI（topview，detailContentView）
extension CMDBMovieDetatilViewController{
    
    private func buildTopView(){
        
        topView                  = UIView.getInstance()
        
        topView.frame            = CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: 0.5 * CMDB_Screen_Width + 64)
        
        topView.backgroundColor  = UIColor.Hex(0x3c3c3c)
        
        contentView?.addSubview(topView)
        
        movieCoverTopView        = UIImageView.getInstance()
        
        movieCoverTopView.frame  = CGRect(x: KleftMargin, y: 64 + 30, width: 105, height: 140)
        
        movieCoverTopView.image  = UIImage(named: "相册背景图")

        topView.addSubview(movieCoverTopView)
        
        // 1.影片名称
        let movieNameLabel       = UILabel.getInstance()
        
        movieNameLabel.frame     = CGRect(x: CGRectGetMaxX(movieCoverTopView.frame) + KleftMargin, y: CGRectGetMinY(movieCoverTopView.frame), width: 200, height: 35)
        movieNameLabel.font      = CMDB_20_Bold_FONT
        
        movieNameLabel.text      = "狼图腾"
        
        movieNameLabel.textColor = whiteColor87
        
        topView.addSubview(movieNameLabel)
        
        // 2.显示地区
        let districLabel         = UILabel.getInstance()
        
        districLabel.text        = "地区 :"
        
        let size                 = districLabel.text!.suitToSize(MaxSize, fontSize: 13, lineSpacing: 1)
        
        districLabel.font        = CMDB_13_FONT
        
        districLabel.textColor   = whiteColor87
        
        districLabel.text        = districLabel.text?.stringByAppendingString("中国大陆")
        
        districLabel.frame       = CGRect(x: CGRectGetMinX(movieNameLabel.frame), y: CGRectGetMaxY(movieNameLabel.frame), width: 200, height: size.height)
        
        topView.addSubview(districLabel)
        
        //3.票房
        let boxOfficeLabel       = UILabel.getInstance()
        
        boxOfficeLabel.textColor = whiteColor87
        
        boxOfficeLabel.font      = CMDB_13_FONT
        
        boxOfficeLabel.text      = "票房 :"
        
        boxOfficeLabel.text      = boxOfficeLabel.text?.stringByAppendingString("3.2亿")
        
        boxOfficeLabel.frame     = CGRect(x: CGRectGetMinX(movieNameLabel.frame), y: CGRectGetMaxY(districLabel.frame) + 5, width: 200, height: size.height)
        
        topView.addSubview(boxOfficeLabel)
        
        //4.类型
        let movieTypesLabel       = UILabel.getInstance()
        
        movieTypesLabel.text      = "类型"
        
        movieTypesLabel.text      = movieTypesLabel.text?.stringByAppendingString("文艺 喜剧 动作")
        
        movieTypesLabel.textColor = whiteColor87
        
        movieTypesLabel.font      = CMDB_13_FONT
        
        movieTypesLabel.frame     = CGRect(x: CGRectGetMinX(movieNameLabel.frame), y: CGRectGetMaxY(boxOfficeLabel.frame) + 5, width: 200, height: size.height)
        
        topView.addSubview(movieTypesLabel)
        
        //5.发布日期
        let publistTimeLabel       = UILabel.getInstance()
        
        publistTimeLabel.text      = "上映日期:"
        
        publistTimeLabel.text      = publistTimeLabel.text?.stringByAppendingString("2016-08-30")
        
        publistTimeLabel.textColor = whiteColor87
        
        publistTimeLabel.font      = CMDB_13_FONT
        
        publistTimeLabel.frame     = CGRect(x: CGRectGetMinX(movieNameLabel.frame), y: CGRectGetMaxY(movieTypesLabel.frame) + 5, width: 200, height: size.height)
        
        topView.addSubview(publistTimeLabel)
        
        //6.片长
        let durationLabel          = UILabel.getInstance()
        
        durationLabel.text         = "片长:"
        
        durationLabel.text         = durationLabel.text?.stringByAppendingString("130分钟")
        
        durationLabel.textColor    = whiteColor87
        
        durationLabel.font         = CMDB_13_FONT
        
        durationLabel.frame        = CGRect(x: CGRectGetMinX(movieNameLabel.frame), y: CGRectGetMaxY(publistTimeLabel.frame) + 5, width: 200, height: size.height)
        
        topView.addSubview(durationLabel)
        
    }
    
    private func buildDeatilContentView(){
        
        detailContentView       = UIView.getInstance()
        
        detailContentView.frame = CGRect(x: 0, y: CGRectGetMaxY(topView.frame), width: CMDB_Screen_Width, height: CMDB_Screen_Height - topView.height)
        
        contentView?.addSubview(detailContentView)
        
        detailContentView.addSubview(pageTitleView)
        
        detailContentView.addSubview(pageContentView)
    }
    
}

// MARK:- PageTitleViewDelegate协议
extension CMDBMovieDetatilViewController : CMDBPageTitleViewDelegate{
    
    func pageTitleView(titleView: CMDBPageTitleView, selectedIndex index: Int) {
        
        pageContentView.setCurrentIndex(index)
    }
}

// MARK:- PageContentViewDelegate协议
extension CMDBMovieDetatilViewController : CMDBPageContentViewDelegate {
    
    func contentView(titleView: CMDBPageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        
        pageTitleView.setTitleWithProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}
